package com.hzlj.position.config.rest.operator;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fz.common.base.core.date.DateUtils;
import com.fz.common.base.exception.PubException;
import com.hzlj.position.config.common.dto.common.PositionAddDTO;
import com.hzlj.position.config.common.dto.operatorActivation.OperatorActivationExtDTO;
import com.hzlj.position.config.common.dto.operatorConfig.OperatorConfigQueryResultDTO;
import com.hzlj.position.config.common.enums.WarningType;

import java.util.Date;
import java.util.function.BiFunction;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;
import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * @author lifh
 * @date 2022/10/12 15:51
 **/
public interface OperatorLocateRest {
    /**
     * 获取位置
     */
    int FETCH_LOCATE_TIMEOUT = 60000;

    /**
     * 根据手机号码获取位置信息
     */
    PositionAddDTO fetchPosition(OperatorConfigQueryResultDTO config,
                                 String jgCode,
                                 String telephone,
                                 BiFunction<String, String, String> dictFun);

    /**
     * 开户
     */
    default void enablePosition(OperatorConfigQueryResultDTO config,
                                OperatorActivationExtDTO ext,
                                String jgCode,
                                String telephone) {
        if (ObjectUtil.isEmpty(config.getLocateEnableRequestUrl())) {
            return;
        }
        throw new PubException(String.format("未实现自动开启运营商定位功能:jgCode=%s,telephone=%s", jgCode, telephone));
    }

    /**
     * 销户
     */
    default void disablePosition(OperatorConfigQueryResultDTO config,
                                 OperatorActivationExtDTO ext,
                                 String jgCode,
                                 String telephone) {
        if (ObjectUtil.isEmpty(config.getLocateDisableRequestUrl())) {
            return;
        }
        throw new PubException(String.format("未实现自动关闭运营商定位功能:jgCode=%s,telephone=%s", jgCode, telephone));
    }

    /**
     * 运营商名称
     */
    String operatorName();

    /**
     * 获取运营商错误码字典
     */
    default String operatorDictType() {
        return null;
    }

    /**
     * 默认位置类
     */
    default PositionAddDTO defaultPosition(String telephone,
                                           OperatorConfigQueryResultDTO config) {
        PositionAddDTO position = new PositionAddDTO();
        position.setId(POSITION_ID_GEN.nextIdStr());
        position.setChannel(config.getChannel());
        position.setChannelType(config.getChannelType());
        position.setPositionTime(DateUtils.parseDate(new Date(), NORM_DATETIME_PATTERN));
        position.setDeviceCode(telephone);
        position.setWarningType(WarningType.SUCCESS.getCode());
        position.setReportType("20");//运营商定位
        position.setPosition(StrUtil.format("【{}】{}", "-1", "定位失败，接口请求有误"));
        return position;
    }
}
